﻿(function () {
    var _inited = false;
    var _handler = "../Handlers/GeneralTypeInfoHandler.ashx";

    function _init() {
        _initUI();
        _initDetailUI();
        _getData();
    }

    function _checkVal() {
        if ($.trim($('#txtfiletypeName', 'table.tbdetail').val()) == "") {
            alert('请输入文件类型名');
            return false;
        }
        return true;
    }

    function _handlerMsg(msg) {
        switch (msg) {
            case 0:
                alert('操作成功');
                _init();
                break;
            case 100:
                alert('文件类型存在');
                break;
            default:
                alert('操作失败');
                break;
        }
    }

    function _initUI() {
        $('table.tbfiletypeinfo tbody').html('');
        $('table.tbfiletypeinfo tbody tr').die().live('click', function () {
            $('table.tbfiletypeinfo tbody tr').removeClass('active');
            $(this).addClass('active');
            _initDetailUI($(this).data('data'));
        });
        $('table.tbfiletypeinfo thead').die().live('click', function () {
            $('table.tbfiletypeinfo tbody tr').removeClass('active');
            _initDetailUI();
        });
        $('#btnAdd', 'table.tbdetail').die().live('click', function () {
            if (_checkVal()) {
                var params = {};
                params.GeneralType = '文件类型';
                params.GeneralValue1 = $.trim($('#txtfiletypeName', 'table.tbdetail').val());
                //params.Description = $.trim($('#txtDescription', 'table.tbdetail').val());
                _addData(params);
            }
        });
        $('#btnDel', 'table.tbdetail').die().live('click', function () {
            var params = $('table.tbdetail').data('data');
            _delData(params);
        });
        $('#btnSave', 'table.tbdetail').die().live('click', function () {
            if (_checkVal()) {
                var params = $('table.tbdetail').data('data');
                params.GeneralType = '文件类型';
                params.GeneralValue1 = $.trim($('#txtfiletypeName', 'table.tbdetail').val());
                //params.Description = $.trim($('#txtDescription', 'table.tbdetail').val());
                _saveData(params);
            }
        });
        $('#btnCancel', 'table.tbdetail').die().live('click', function () {
            $('table.tbfiletypeinfo tbody tr').removeClass('active');
            _initDetailUI();
        });
    }

    function _initDetailUI(cd) {
        if (cd && cd != null) {
            $('#txtfiletypeName', 'table.tbdetail').val(cd.GeneralValue1);
            //$('#txtDescription', 'table.tbdetail').val(cd.Description);
            $('#btnAdd', 'table.tbdetail').attr('disabled', 'disabled');
            $('#btnDel', 'table.tbdetail').removeAttr('disabled');
            $('#btnSave', 'table.tbdetail').removeAttr('disabled');
            $('table.tbdetail').data('data', cd);
        }
        else {
            $('#txtfiletypeName', 'table.tbdetail').val('');
            //$('#txtDescription', 'table.tbdetail').val('');
            $('#btnAdd', 'table.tbdetail').removeAttr('disabled');
            $('#btnDel', 'table.tbdetail').attr('disabled', 'disabled');
            $('#btnSave', 'table.tbdetail').attr('disabled', 'disabled');
            $('table.tbdetail').data('data', null);
        }
    }

    function _getData() {
        $.ajax({
            type: "get",
            url: _handler + "?action=GetGeneralTypeInfo" + '&Params='+encodeURIComponent('文件类型') + "&ts=" + new Date().valueOf(),
            dataType: 'json',
            async: false,
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _bindData(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _bindData(data) {
        if (data) {
            for (var i = 0; i < data.length; i++) {
                var d = data[i];
                var tr = '<tr>';
                tr += '<td>';
                tr += (i + 1);
                tr += '</td>';
                tr += '<td>';
                tr += d.GeneralValue1;
                tr += '</td>';
                tr += '</tr>';
                $('table.tbfiletypeinfo tbody').append($(tr).data('data', d));
            }
        }
    }

    function _addData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=AddGeneralTypeInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _delData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=DelGeneralTypeInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _saveData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=SaveGeneralTypeInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    this.FileTypeInfoMgr = {
        init:
        function () {
            if (!!_inited) return;
            _init();
            _inited = true;
        }
    }
})();
$(function () {
    FileTypeInfoMgr.init();
});